package com.smarthealthring.mm.widgets;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.smarthealthring.mm.App;
import com.smarthealthring.mm.R;

/**
 * Created by hewei26 on 2017/12/27.
 */

public class NoticeView {

    private final PopupWindow mPopupWindow;

    public NoticeView(Context context, View anchorView, NoticeRefreshListener listener) {
        View noticeView = LayoutInflater.from(context).inflate(R.layout.notice_view, null);
        mPopupWindow = new PopupWindow(noticeView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setFocusable(false);
        mPopupWindow.setOutsideTouchable(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mPopupWindow.setElevation(20.0f);
        }


        noticeView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = noticeView.getMeasuredWidth();    //  获取测量后的宽度
        int popupHeight = noticeView.getMeasuredHeight();  //获取测量后的高度

        mPopupWindow.showAsDropDown(anchorView, App.SCREEN_WIDTH / 2 - popupWidth / 2, 100 - anchorView.getHeight());

        //        popupWindow.showAtLocation(anchorView, Gravity.LEFT | Gravity.TOP, App.SCREEN_WIDTH/2 -popupWidth / 2, App.SCREEN_HEIGHT / 4);


        noticeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();

                    listener.noticeRefresh();
                }
            }
        });
    }


    public void dismiss(){
        if(mPopupWindow != null){
            mPopupWindow.dismiss();
        }
    }


    public static NoticeView generateNoticeView(Context context, View anchorView, NoticeRefreshListener listener) {
        return new NoticeView(context, anchorView, listener);
    }


    public interface NoticeRefreshListener {
        void noticeRefresh();
    }
}
